using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

namespace ProjectEuler.Core
{
    public class Problem30 : IProjectEulerProblem
    {
        protected IList<int> SumOfDigitsToPower(int power)
        {
            var list = new List<int>();
            for(int i=1000;i<=999999;i++)
            {
                IEnumerable<int> digits = GetDigits(i);
                var sumOfPowers = digits.Select(d => (int)Math.Pow(d, power)).Sum();
                if(sumOfPowers == i)
                {
                    list.Add(i);
                }
            }
            return list;
        }

        private static IEnumerable<int> GetDigits(int i)
        {
            return i.ToString().Select(c => int.Parse(c.ToString()));
        }

        public int Number
        {
            get { return 30; }
        }

        public string Description
        {
            get { return "Find the sum of all the numbers that can be written as the sum of fifth powers of their digits."; }
        }

        public string Answer
        {
            get { return SumOfDigitsToPower(5).Sum().ToString(); }
        }
    }

    [TestFixture]
    public class Problem30Tests : Problem30
    {
        [Test]
        public void Sum_of_fourth_powers_should_be_19316()
        {
            Assert.AreEqual(19316, SumOfDigitsToPower(4).Sum());
        }
    }
}